package org.example.demo.service.userService.impl;

import org.example.demo.dto.CommentParam;
import org.example.demo.service.userService.CommentService;
import org.example.mbg.mapper.CommentMapper;
import org.example.mbg.mapper.UserOrderMapper;
import org.example.mbg.model.Comment;
import org.example.mbg.model.CommentExample;
import org.example.mbg.model.UserOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UserOrderMapper userOrderMapper;

    @Override
    public int add(CommentParam commentParam) {
        Comment comment = new Comment();
        Date date = new Date();
        comment.setUserId(commentParam.getUser_id());
        comment.setProductId(commentParam.getProduct_id());
        comment.setContent(commentParam.getContent());
        comment.setShopId(commentParam.getShop_id());
        comment.setCreateTime(date);
        comment.setStyle("3t");

        UserOrder u = userOrderMapper.selectByPrimaryKey(commentParam.getOrder_id());
        u.setCommentTime(date);
        u.setStatus(2);
        userOrderMapper.updateByPrimaryKeySelective(u);

        return commentMapper.insertSelective(comment);
    }

    @Override
    public List<Comment> getCommentOfProduct(Long productId) {
        CommentExample commentExample = new CommentExample();
        commentExample.createCriteria().andProductIdEqualTo(productId);
        return commentMapper.selectByExample(commentExample);
    }

    @Override
    public List<Comment> getCommentOfUser(Long userId) {
        CommentExample commentExample = new CommentExample();
        commentExample.createCriteria().andUserIdEqualTo(userId);
        return commentMapper.selectByExample(commentExample);
    }
}
